<template>
	<view>
		<image :src="msg.order_goods[0].goods_image" style="width: 100%;" mode="widthFix"></image>

		<view class="a">
			<view class="money">
				<text>￥</text>
				<text>{{msg.total_amount}}</text>
				<!-- <text>起</text> -->
			</view>
			<view class="title">{{msg.order_goods[0].goods_name}}</view>
			<view class="message">套餐描述：{{msg.detail?msg.detail:'无'}}</view>
			<view class="uniBtn" v-if="timestamp>msg.start_time && timestamp<msg.end_time">
				<text @click="sub(1)" class="tuikuan">开门</text>
				<text @click="sub(2)" class="tuikuan">播放投影</text>
			</view>
		</view>

		<view class="b">
			<view class="b1">
				<text>房间位置：</text>
				<text style="margin-right: 20rpx;">{{msg.fangjian.address}}</text>
				<text>{{msg.fangjian.title}}</text>
			</view>
			<view class="b1">
				<text>使用时间：</text>
				<text style="margin-right: 20rpx;">{{msg.yytime}}</text>
				<text>{{msg.order_goods[0].spec_value_str}}</text>
			</view>
			<view class="b1">
				<text>订单号码：</text>
				<text>{{id}} </text>
			</view>
			<view class="b1">
				<text>下单时间：</text>
				<text>{{msg.pay_time?msg.pay_time:''}}</text>
			</view>
			<view class="b1">
				<text>订单状态：</text>
				<text>{{msg.order_status_desc}}</text>
			</view>
			<view class="img">
				<view class="shangchuan" v-if="msg.order_status==3 && isshow!=1 && pingjia==0">
					<u-button @click="upload" :plain="true" type="primary" text="上传图片"></u-button>
				</view>
				<view v-for="(item, i) in tempFilePaths" :key="i" style="position: relative;">
					<image :class="[(i+1)%4 === 0 ?'noMargin':'']" :src="item" @click="previewImage(i)" mode="">
					</image>
					<image v-if="msg.order_status==3 && isshow!=1 && pingjia==0" :class="[(i+1)%4 === 0 ?'noMargindel':'']" class="del" src="@/static/mains/del.png"
						mode="" @click="delImg(i)"></image>
				</view>
			</view>
			
			<view class="img">
				<view class="shangchuan" v-if="msg.order_status==3 && isshow!=1 && pingjia==0">
					<u-button @click="uploadVideo" :plain="true" type="primary" text="上传视频"></u-button>
				</view>
				<view v-for="(item, i) in tempFilePathsVideos" :key="i" style="position: relative;">
					<video :src="item"></video>
					<image v-if="msg.order_status==3 && isshow!=1 && pingjia==0" class="noMargindel2 del" src="@/static/mains/del.png"
						mode="" @click="delVideo(i)"></image>
				</view>
			</view>
			
		</view>
		<view class="base" v-if="msg.order_status==3 && isshow!=1 && pingjia==0">
			<u-button @click="submit" type="primary" text="提交"></u-button>
		</view>
		
		<u-popup border-radius='10' :show="show" mode="center" @close="show=false" closeable>
			<view class="popupmsg">
				<view class="popupbox1">退款</view>
				
				<view class="popupbox2">
					<u--textarea v-model="content" height="130" placeholder="请填写退款原因..." count maxlength="100"></u--textarea>
				</view>

				<view class="other-button" hover-class="other-button-hover" @click="apply">提交</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import { uploadFile,uploadFileVideo } from '@/utils/tools';
	export default {
		data() {
			return {
				timer:null,
				show:false,
				content:'',
				id:'',
				msg:{
					order_goods:[{goods_image:''}],
					start_time:0,
					end_time:0,  
				},
				timestamp:0,
				tempFilePaths:[],
				tempFilePathsVideos:[],
				isshow:0,
				pingjia:0,
			}
		},
		async onLoad(options) {
			this.id = options.id;
			this.pingjia = options.pingjia;
			let res = await this.$api.orderdetail({
				id: options.id
			});
			if (res.code == 1) {
				this.getTime()
				this.timer = setInterval(() => {
					this.getTime()
				}, 180000); 	
				if(res.data.file_image.length>0){
					res.data.file_image.forEach(item=>{
						this.tempFilePaths.push(item.url)
					})
				}
				if(res.data.file_video.length>0){
					res.data.file_video.forEach(item=>{
						this.tempFilePathsVideos.push(item.url)
					})
				}
				this.isshow = res.data.is_upfile;
				this.msg = res.data;
			}
		},
		methods: {
			// 获取当前时间戳
			getTime(){
				const timestamp = Math.round(new Date().getTime()/1000).toString();
				this.timestamp = Number(timestamp);
				console.log(this.timestamp,'当前时间戳')
			},
			
			// 退款
			refund() {
				this.show = true;
			},
			// 开门和播放投影
			async sub(num){
				if(num==1){
					let params ={
						id:this.id
					}
					uni.showLoading()
					let res = await this.$api.orderopen(params);
					if(res.code == 1){
						// console.log(res)
						uni.hideLoading()
						uni.showToast({
							title:'操作成功'
						})
					}
				}else{
					let params ={
						id:this.id
					}
					uni.showLoading()
					let res = await this.$api.orderdstart(params);
					if(res.code == 1){
						// console.log(res)
						uni.hideLoading()
						uni.showToast({
							title:'播放成功'
						})
					}
				}
			},
			// 提交退款申请
			apply(){
				this.show = false;
				uni.showModal({
					title: '温馨提示',
					content: '您的退款申请已提交 请注意查看退款进度哦~！',
					cancelColor: '#B3B3B3',
					confirmText: '确定',
					confirmColor: '#E2867E',
					showCancel:false,
					success: function(res) {
						if (res.confirm) {
							uni.navigateBack()
						} else if (res.cancel) {
							
						}
					}
				});
			},
			// 上传
			upload() {
				var that = this;
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: function (res) {
						let list = [];
						let count = 0;
						let files = res.tempFilePaths;
						uni.showLoading();
						files.forEach((item) => {
							uploadFile(item, {
								header: {
									'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
								}
							})
								.then((res) => {
									uni.hideLoading();
									count++;
									//如果是最后一张,则隐藏等待中
									if (count == files.length) {
										wx.hideLoading();
									}
									// console.log(res,'1111');
									// this.avatar = res;
									list.push(res); //待上传图片临时路径
									that.tempFilePaths = list;
								})
								.catch(() => {
									uni.hideLoading();
									uni.showToast({
										title: '上传失败,请重新上传!',
										icon: 'none'
									});
								});
						});
					}
				});
			},
			// 删除
			delImg(i) {
				this.tempFilePaths.splice(i, 1);
			},
			
			// 预览
			previewImage: function(index) {
				let imgs = []
				for(let i=0;i<this.tempFilePaths.length;i++){
					let str = this.tempFilePaths[i]
					imgs.push(str)
				}
				uni.previewImage({
					current: index, //预览图片的下标
					urls: imgs //预览图片的地址，必须要数组形式，如果不是数组形式就转换成数组形式就可以
				})
			},
			// 上传视频
			uploadVideo() {
				var that = this;
				uni.chooseVideo({
					sourceType: ['album'], //从相册选择
					success: function (res) {
						let files = res.tempFilePath;
						uni.showLoading();
						uploadFileVideo(files, {
							header: {
								'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
							}
						})
							.then((res) => {
								uni.hideLoading();
								that.tempFilePathsVideos.push(res); //待上传视频临时路径
								console.log(that.tempFilePathsVideos)
							})
							.catch(() => {
								uni.hideLoading();
								uni.showToast({
									title: '上传失败,请重新上传!',
									icon: 'none'
								});
							});
					}
				});
			},
			// 删除
			delVideo(i) {
				this.tempFilePathsVideos.splice(i, 1);
			},
			async submit(){
				if(this.tempFilePaths.length==0 && this.tempFilePathsVideos.length==0){
					uni.showToast({
						title:'请至少上传一次图片或者视频',
						icon:'none'
					})
					return
				}
				let params ={
					order_id:this.id,
					image:this.tempFilePaths.toString(),
					video:this.tempFilePathsVideos.toString()
				}
				let res = await this.$api.uploadFileMsg(params);
				if(res.code == 1){
					// console.log(res)
					uni.showToast({
						title:'操作成功'
					})
					setTimeout(function(){
						uni.navigateBack()
					},1000)
				}
			},
		},
		onHide() {
			if (this.timer) {
				//如果定时器还在运行 或者直接关闭，不用判断
				clearInterval(this.timer); //关闭
			}
		},
		onUnload() {
			if (this.timer) {
				//如果定时器还在运行 或者直接关闭，不用判断
				clearInterval(this.timer); //关闭
			}
		}
	}
</script>

<style scoped lang="less">
	.popupmsg {
		width: 608rpx;
		background: #FFFFFF;
		border-radius:20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;

		.popupbox1 {
			height: 104rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #454545;
		}

		.popupbox2{
			width: 520rpx;
			background: #fff;
			padding: 30rpx;
			border-radius: 25rpx;
		}

		.other-button {
			width: 400rpx;
			height: 80rpx;
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #ffffff;
			margin-top: 30rpx;
		}
	}

	.a {
		margin: 20rpx;
		padding: 30rpx 20rpx;
		background: #fff;
		border-radius: 15rpx;

		.money {
			font-weight: bold;
			font-size: 28rpx;
			color: #AA3128;

			text {
				&:nth-child(2) {
					font-size: 48rpx;
					margin: 0 10rpx;
				}
			}
		}

		.title {
			color: #080808;
			font-size: 30rpx;
			margin: 15rpx 0;
			font-weight: bold;
		}

		.message {
			font-weight: 400;
			color: #222222;
		}

		.tuikuan {
			text-align: center;
			width: 180rpx;
			height: 60rpx;
			line-height: 58rpx;
			border-radius: 35rpx;
			font-size: 30rpx;
			font-weight: 400;
			background: #0070D9;
			color: #fff;
			margin-top: 25rpx;
		}
	}

	.b {
		margin: 20rpx;
		padding: 30rpx 20rpx;
		background: #fff;
		border-radius: 15rpx;

		.b1 {
			margin: 15rpx 45rpx;

			text {
				&:nth-child(1) {
					color: #6F6F6F;
				}

				&:nth-child(2) {
					font-size: 30rpx;
					font-weight: bold;
					color: #000000;
				}
			}
		}
	}
	.img {
		display: flex;
		flex-wrap: wrap;
		background: #fff;
		padding: 30rpx;
		border-radius: 25rpx;
		
		.shangchuan{
			color: #0070D9;
			margin-bottom: 30rpx;
			font-weight: bold;
			width: 100%;
		}
		
		.del {
			width: 40rpx;
			height: 40rpx;
			position: absolute;
			top: -20rpx;
			right: -20rpx;
		}
	}
	video{
		height: 300rpx;
		flex: 1;
	}
	.noMargindel2 {
		top: 10rpx !important;
		right: -10rpx !important;
	}
	image {
		width: 130rpx;
		height: 130rpx;
		margin-bottom: 20rpx;
		margin-right: 25rpx;
	}
	
	.noMargin {
		margin-right: 0rpx;
	}
	
	.noMargindel {
		top: -20rpx !important;
		right: -22rpx !important;
	}
	
	.upload {
		width: 140rpx;
		height: 140rpx;
		background: url(@/static/mains/m8.png) no-repeat;
		background-size: 100% 100%;
		margin-bottom: 20rpx;
	}
	.base{
		flex: 1;
		margin: 30rpx;
	}
</style>